package {{package}};

import java.net.URI;

import io.helidon.security.EndpointConfig;
import io.helidon.security.Security;
import io.helidon.security.providers.httpauth.HttpBasicAuthProvider;
import io.helidon.webclient.http1.Http1Client;
import io.helidon.webclient.http1.Http1ClientResponse;
import io.helidon.webclient.security.WebClientSecurity;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpServer;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
 * Test of security override example.
 */
@ServerTest
public class OutboundOverrideJwtTest {

    private final Http1Client client;

    OutboundOverrideJwtTest(WebServer server, URI uri) {
        server.context().register(server);
        Security security = Security.builder()
                .addProvider(HttpBasicAuthProvider.builder().build())
                .build();
        client = Http1Client.builder()
                .baseUri(uri)
                .addService(WebClientSecurity.create(security))
                .build();
    }

    @SetUpServer
    public static void setup(WebServerConfig.Builder server) {
        OutboundOverrideJwt.setup(server);
    }

    @Test
    public void testOverrideExample() {
        try (Http1ClientResponse response = client.get()
                .path("/override")
                .property(EndpointConfig.PROPERTY_OUTBOUND_ID, "jack")
                .property(EndpointConfig.PROPERTY_OUTBOUND_SECRET, "changeit")
                .request()) {

            assertThat(response.status().code(), is(200));

            String entity = response.entity().as(String.class);
            assertThat(entity, is("You are: jack, backend service returned: jill"));
        }
    }

    @Test
    public void testPropagateExample() {
        try (Http1ClientResponse response = client.get()
                .path("/propagate")
                .property(EndpointConfig.PROPERTY_OUTBOUND_ID, "jack")
                .property(EndpointConfig.PROPERTY_OUTBOUND_SECRET, "changeit")
                .request()) {

            assertThat(response.status().code(), is(200));

            String entity = response.entity().as(String.class);
            assertThat(entity, is("You are: jack, backend service returned: jack"));
        }

    }
}
